package com.example.kliven.because.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.example.kliven.because.R;
import com.example.kliven.because.bean.MostTopicBean;
import com.example.kliven.because.imagecache.DoubleCache;

import java.util.List;

/**
 * Created by Kliven on 16/1/14.
 */
public class MostTopicAdapter extends BaseAdapter {

    private List<MostTopicBean> mostTopicBeans;
    private Context context;
    private RequestQueue queue;

    public MostTopicAdapter(List<MostTopicBean> mostTopicBeans, Context context) {
        this.mostTopicBeans = mostTopicBeans;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mostTopicBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return mostTopicBeans;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        MostTopicViewHodler mostTopicViewHodler;
        if (view == null) {
            mostTopicViewHodler = new MostTopicViewHodler();
            view = LayoutInflater.from(context).inflate(R.layout.item_lv_mosttopic,viewGroup,false);
            mostTopicViewHodler.share_tag_name = (TextView) view.findViewById(R.id.share_tag_name);
            mostTopicViewHodler.share_num = (TextView) view.findViewById(R.id.share_num);
            mostTopicViewHodler.image_url = (NetworkImageView) view.findViewById(R.id.image_url);
            view.setTag(mostTopicViewHodler);
        } else {
            mostTopicViewHodler = (MostTopicViewHodler) view.getTag();
        }
            mostTopicViewHodler.share_tag_name.setText(mostTopicBeans.get(i).getShare_tag_name());
            mostTopicViewHodler.share_num.setText(mostTopicBeans.get(i).getShare_num());
        queue = Volley.newRequestQueue(context);

        DoubleCache cache = new DoubleCache();
        ImageLoader imageLoader = new ImageLoader(queue, cache);
        mostTopicViewHodler.image_url.setDefaultImageResId(R.mipmap.moren);
        mostTopicViewHodler.image_url.setErrorImageResId(R.mipmap.moren);
        mostTopicViewHodler.image_url.setImageUrl(mostTopicBeans.get(i).getImage_url(), imageLoader);

        return view;
    }

    static class MostTopicViewHodler{
        NetworkImageView image_url;
        TextView share_tag_name,share_num;
    }
}

